<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children' }"
    >
      <el-table-column prop="id" label="规格编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="specsname" label="规格名称" sortable width="180">
      </el-table-column>
       <el-table-column label="规格属性">
          <template v-slot="scope">
              <el-tag v-for="item in scope.row.attrs" :key="item">{{item}}</el-tag>
          </template>
      </el-table-column>

      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="success"
            >启用</el-button
          >
          <el-button v-else type="danger"> 禁用 </el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="cPage"
      :page-size="size"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { speceDel } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  computed: {
    ...mapGetters({
      tableData: "goodsSpec/specs",
      size: "goodsSpec/size",
      total: "goodsSpec/count",
    }),
  },
  methods: {
    ...mapActions({
      specListActions: "goodsSpec/specListActions",
      requestCount: "goodsSpec/countActions",
      requestPage: "goodsSpec/pageActions",
    }),
    cPage(page) {
      this.requestPage(page);
      this.specListActions();
    },
    del(id) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 确定要删除
          speceDel({ id }).then((res) => {
            if (res.code === 200) {
              successAlert(res.msg);
              this.requestCount();
              this.specListActions();
            }
          });
        })
        .catch(() => {
          // 取消删除
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    edit(id){
      this.$emit("edit",id);
    }
  },
  mounted() {
    this.requestCount();
    this.specListActions();
  },
};
</script>

<style>
</style>